

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>nlp_architect.models.np2vec &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
        <script type="text/javascript" src="../../../_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../transformers.html">Transformers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../models.html">nlp_architect.models</a> &raquo;</li>
        
      <li>nlp_architect.models.np2vec</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for nlp_architect.models.np2vec</h1><div class="highlight"><pre>
<span></span><span class="c1"># ******************************************************************************</span>
<span class="c1"># Copyright 2017-2018 Intel Corporation</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#     http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ******************************************************************************</span>

<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="kn">from</span> <span class="nn">gensim.models</span> <span class="kn">import</span> <span class="n">FastText</span><span class="p">,</span> <span class="n">Word2Vec</span><span class="p">,</span> <span class="n">KeyedVectors</span>
<span class="kn">from</span> <span class="nn">gensim.models.word2vec</span> <span class="kn">import</span> <span class="n">LineSentence</span>
<span class="kn">from</span> <span class="nn">gensim</span> <span class="kn">import</span> <span class="n">utils</span>
<span class="kn">import</span> <span class="nn">nltk</span>
<span class="kn">from</span> <span class="nn">nltk.corpus</span> <span class="kn">import</span> <span class="n">conll2000</span>
<span class="kn">from</span> <span class="nn">six</span> <span class="kn">import</span> <span class="n">iteritems</span>
<span class="kn">from</span> <span class="nn">smart_open</span> <span class="kn">import</span> <span class="nb">open</span> <span class="k">as</span> <span class="n">smart_open</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<span class="c1"># pylint: disable-msg=too-many-instance-attributes</span>
<div class="viewcode-block" id="NP2vec"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.np2vec.NP2vec">[docs]</a><span class="k">class</span> <span class="nc">NP2vec</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Initialize the np2vec model, train it, save it and load it.</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="NP2vec.is_marked"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.np2vec.NP2vec.is_marked">[docs]</a>    <span class="k">def</span> <span class="nf">is_marked</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check if a string is marked.</span>

<span class="sd">        Args:</span>
<span class="sd">            s (str): string to check</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">s</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">mark_char</span></div>

    <span class="c1"># pylint: disable-msg=too-many-arguments</span>
    <span class="c1"># pylint: disable-msg=too-many-locals</span>
    <span class="c1"># pylint: disable-msg=too-many-branches</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>  <span class="c1"># noqa: C901</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">corpus</span><span class="p">,</span>
        <span class="n">corpus_format</span><span class="o">=</span><span class="s2">&quot;txt&quot;</span><span class="p">,</span>
        <span class="n">mark_char</span><span class="o">=</span><span class="s2">&quot;_&quot;</span><span class="p">,</span>
        <span class="n">word_embedding_type</span><span class="o">=</span><span class="s2">&quot;word2vec&quot;</span><span class="p">,</span>
        <span class="n">sg</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
        <span class="n">size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
        <span class="n">window</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
        <span class="n">alpha</span><span class="o">=</span><span class="mf">0.025</span><span class="p">,</span>
        <span class="n">min_alpha</span><span class="o">=</span><span class="mf">0.0001</span><span class="p">,</span>
        <span class="n">min_count</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>
        <span class="n">sample</span><span class="o">=</span><span class="mf">1e-5</span><span class="p">,</span>
        <span class="n">workers</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span>
        <span class="n">hs</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
        <span class="n">negative</span><span class="o">=</span><span class="mi">25</span><span class="p">,</span>
        <span class="n">cbow_mean</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
        <span class="n">iterations</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span>
        <span class="n">min_n</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
        <span class="n">max_n</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span>
        <span class="n">word_ngrams</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
        <span class="n">prune_non_np</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initialize np2vec model and train it.</span>

<span class="sd">        Args:</span>
<span class="sd">          corpus (str): path to the corpus.</span>
<span class="sd">          corpus_format (str {json,txt,conll2000}): format of the input marked corpus; txt and json</span>
<span class="sd">          formats are supported. For json format, the file should contain an iterable of</span>
<span class="sd">          sentences. Each sentence is a list of terms (unicode strings) that will be used for</span>
<span class="sd">          training.</span>
<span class="sd">          mark_char (char): special character that marks NP&#39;s suffix.</span>
<span class="sd">          word_embedding_type (str {word2vec,fasttext}): word embedding model type; word2vec and</span>
<span class="sd">          fasttext are supported.</span>
<span class="sd">          np2vec_model_file (str): path to the file where the trained np2vec model has to be</span>
<span class="sd">          stored.</span>
<span class="sd">          binary (bool): boolean indicating whether the model is stored in binary format; if</span>
<span class="sd">          word_embedding_type is fasttext and word_ngrams is 1, binary should be set to True.</span>
<span class="sd">          sg (int {0,1}): model training hyperparameter, skip-gram. Defines the training</span>
<span class="sd">          algorithm. If 1, CBOW is used,otherwise, skip-gram is employed.</span>
<span class="sd">          size (int): model training hyperparameter, size of the feature vectors.</span>
<span class="sd">          window (int): model training hyperparameter, maximum distance between the current and</span>
<span class="sd">          predicted word within a sentence.</span>
<span class="sd">          alpha (float): model training hyperparameter. The initial learning rate.</span>
<span class="sd">          min_alpha (float): model training hyperparameter. Learning rate will linearly drop to</span>
<span class="sd">          `min_alpha` as training progresses.</span>
<span class="sd">          min_count (int): model training hyperparameter, ignore all words with total frequency</span>
<span class="sd">          lower than this.</span>
<span class="sd">          sample (float): model training hyperparameter, threshold for configuring which</span>
<span class="sd">          higher-frequency words are randomly downsampled, useful range is (0, 1e-5)</span>
<span class="sd">          workers (int): model training hyperparameter, number of worker threads.</span>
<span class="sd">          hs (int {0,1}): model training hyperparameter, hierarchical softmax. If set to 1,</span>
<span class="sd">          hierarchical softmax will be used for model training. If set to 0, and `negative` is non-</span>
<span class="sd">                        zero, negative sampling will be used.</span>
<span class="sd">          negative (int): model training hyperparameter, negative sampling. If &gt; 0, negative</span>
<span class="sd">          sampling will be used, the int for negative specifies how many &quot;noise words&quot; should be</span>
<span class="sd">          drawn (usually between 5-20). If set to 0, no negative sampling is used.</span>
<span class="sd">          cbow_mean (int {0,1}): model training hyperparameter. If 0, use the sum of the context</span>
<span class="sd">          word vectors. If 1, use the mean, only applies when cbow is used.</span>
<span class="sd">          iterations (int): model training hyperparameter, number of iterations.</span>
<span class="sd">          min_n (int): fasttext training hyperparameter. Min length of char ngrams to be used</span>
<span class="sd">          for training word representations.</span>
<span class="sd">          max_n (int): fasttext training hyperparameter. Max length of char ngrams to be used for</span>
<span class="sd">          training word representations. Set `max_n` to be lesser than `min_n` to avoid char</span>
<span class="sd">          ngrams being used.</span>
<span class="sd">          word_ngrams (int {0,1}): fasttext training hyperparameter. If 1, uses enrich word</span>
<span class="sd">          vectors with subword (ngrams) information. If 0, this is equivalent to word2vec training.</span>
<span class="sd">          prune_non_np (bool): indicates whether to prune non-NP&#39;s after training process.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">mark_char</span> <span class="o">=</span> <span class="n">mark_char</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">word_embedding_type</span> <span class="o">=</span> <span class="n">word_embedding_type</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sg</span> <span class="o">=</span> <span class="n">sg</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="n">size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">window</span> <span class="o">=</span> <span class="n">window</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">alpha</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_alpha</span> <span class="o">=</span> <span class="n">min_alpha</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_count</span> <span class="o">=</span> <span class="n">min_count</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sample</span> <span class="o">=</span> <span class="n">sample</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">workers</span> <span class="o">=</span> <span class="n">workers</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hs</span> <span class="o">=</span> <span class="n">hs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">negative</span> <span class="o">=</span> <span class="n">negative</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cbow_mean</span> <span class="o">=</span> <span class="n">cbow_mean</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iter</span> <span class="o">=</span> <span class="n">iterations</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_n</span> <span class="o">=</span> <span class="n">min_n</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_n</span> <span class="o">=</span> <span class="n">max_n</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">word_ngrams</span> <span class="o">=</span> <span class="n">word_ngrams</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prune_non_np</span> <span class="o">=</span> <span class="n">prune_non_np</span>

        <span class="k">if</span> <span class="n">corpus_format</span> <span class="o">==</span> <span class="s2">&quot;txt&quot;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sentences</span> <span class="o">=</span> <span class="n">LineSentence</span><span class="p">(</span><span class="n">corpus</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">corpus_format</span> <span class="o">==</span> <span class="s2">&quot;json&quot;</span><span class="p">:</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">corpus</span><span class="p">)</span> <span class="k">as</span> <span class="n">json_data</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sentences</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">json_data</span><span class="p">)</span>
        <span class="c1"># pylint: disable-msg=too-many-nested-blocks</span>
        <span class="k">elif</span> <span class="n">corpus_format</span> <span class="o">==</span> <span class="s2">&quot;conll2000&quot;</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sentences</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">chunked_sent</span> <span class="ow">in</span> <span class="n">conll2000</span><span class="o">.</span><span class="n">chunked_sents</span><span class="p">(</span><span class="n">corpus</span><span class="p">):</span>
                    <span class="n">tokens</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
                    <span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">chunked_sent</span><span class="p">:</span>
                        <span class="c1"># pylint: disable-msg=protected-access</span>
                        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">chunk</span><span class="p">,</span> <span class="s2">&quot;_label&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">chunk</span><span class="o">.</span><span class="n">_label</span> <span class="o">==</span> <span class="s2">&quot;NP&quot;</span><span class="p">:</span>
                            <span class="n">s</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
                            <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">chunk</span><span class="p">:</span>
                                <span class="n">s</span> <span class="o">+=</span> <span class="n">w</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">mark_char</span>
                            <span class="n">tokens</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">chunk</span><span class="p">,</span> <span class="n">nltk</span><span class="o">.</span><span class="n">Tree</span><span class="p">):</span>
                                <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">chunk</span><span class="p">:</span>
                                    <span class="n">tokens</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">w</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">tokens</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">chunk</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_sentences</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tokens</span><span class="p">)</span>
            <span class="c1"># pylint: disable-msg=broad-except</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Conll2000 dataset is missing. See downloading details in the &quot;</span> <span class="s2">&quot;README file&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;invalid corpus format: </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">corpus_format</span><span class="p">)</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">word_embedding_type</span> <span class="o">==</span> <span class="s2">&quot;fasttext&quot;</span> <span class="ow">and</span> <span class="n">word_ngrams</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="c1"># remove the marking character at the end for subword fasttext model training</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sentences</span> <span class="o">=</span> <span class="p">[</span>
                <span class="p">[</span><span class="n">w</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_marked</span><span class="p">(</span><span class="n">w</span><span class="p">)</span> <span class="k">else</span> <span class="n">w</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">sentence</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">sentence</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sentences</span>
            <span class="p">]</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;training np2vec model&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_train</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_train</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Train the np2vec model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">word_embedding_type</span> <span class="o">==</span> <span class="s2">&quot;word2vec&quot;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">Word2Vec</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sentences</span><span class="p">,</span>
                <span class="n">sg</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sg</span><span class="p">,</span>
                <span class="n">size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">,</span>
                <span class="n">window</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">window</span><span class="p">,</span>
                <span class="n">alpha</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">,</span>
                <span class="n">min_alpha</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">min_alpha</span><span class="p">,</span>
                <span class="n">min_count</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">min_count</span><span class="p">,</span>
                <span class="n">sample</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sample</span><span class="p">,</span>
                <span class="n">workers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">workers</span><span class="p">,</span>
                <span class="n">hs</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">hs</span><span class="p">,</span>
                <span class="n">negative</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">negative</span><span class="p">,</span>
                <span class="n">cbow_mean</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cbow_mean</span><span class="p">,</span>
                <span class="nb">iter</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">iter</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">word_embedding_type</span> <span class="o">==</span> <span class="s2">&quot;fasttext&quot;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">FastText</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_sentences</span><span class="p">,</span>
                <span class="n">sg</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sg</span><span class="p">,</span>
                <span class="n">size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">,</span>
                <span class="n">window</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">window</span><span class="p">,</span>
                <span class="n">alpha</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">,</span>
                <span class="n">min_alpha</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">min_alpha</span><span class="p">,</span>
                <span class="n">min_count</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">min_count</span><span class="p">,</span>
                <span class="n">sample</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sample</span><span class="p">,</span>
                <span class="n">workers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">workers</span><span class="p">,</span>
                <span class="n">hs</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">hs</span><span class="p">,</span>
                <span class="n">negative</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">negative</span><span class="p">,</span>
                <span class="n">cbow_mean</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cbow_mean</span><span class="p">,</span>
                <span class="nb">iter</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">iter</span><span class="p">,</span>
                <span class="n">min_n</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">min_n</span><span class="p">,</span>
                <span class="n">max_n</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">max_n</span><span class="p">,</span>
                <span class="n">word_ngrams</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">word_ngrams</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;invalid word embedding type: </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">word_embedding_type</span><span class="p">)</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

<div class="viewcode-block" id="NP2vec.save"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.np2vec.NP2vec.save">[docs]</a>    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">np2vec_model_file</span><span class="o">=</span><span class="s2">&quot;np2vec.model&quot;</span><span class="p">,</span> <span class="n">binary</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">word2vec_format</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Save the np2vec model.</span>

<span class="sd">        Args:</span>
<span class="sd">            np2vec_model_file (str): the file containing the np2vec model to load</span>
<span class="sd">            binary (bool): boolean indicating whether the np2vec model to load is in binary format</span>
<span class="sd">            word2vec_format(bool): boolean indicating whether to save the model in original</span>
<span class="sd">            word2vec format.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">word_embedding_type</span> <span class="o">==</span> <span class="s2">&quot;fasttext&quot;</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">word_ngrams</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">binary</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                    <span class="s2">&quot;if word_embedding_type is fasttext and word_ngrams is 1, &quot;</span>
                    <span class="s2">&quot;binary should be set to True.&quot;</span>
                <span class="p">)</span>
                <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
            <span class="c1"># not relevant to prune fasttext subword model</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">np2vec_model_file</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># prune non NP terms</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">prune_non_np</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;pruning np2vec model&quot;</span><span class="p">)</span>
                <span class="n">total_vec</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">vector_size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">vector_size</span>
                <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">wv</span><span class="o">.</span><span class="n">vocab</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_marked</span><span class="p">(</span><span class="n">word</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">word</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                        <span class="n">total_vec</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                    <span class="s2">&quot;storing </span><span class="si">%s</span><span class="s2">x</span><span class="si">%s</span><span class="s2"> projection weights for NP&#39;s into </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span>
                    <span class="n">total_vec</span><span class="p">,</span>
                    <span class="n">vector_size</span><span class="p">,</span>
                    <span class="n">np2vec_model_file</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="k">with</span> <span class="n">smart_open</span><span class="p">(</span><span class="n">np2vec_model_file</span><span class="p">,</span> <span class="s2">&quot;wb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fout</span><span class="p">:</span>
                    <span class="n">fout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">utils</span><span class="o">.</span><span class="n">to_utf8</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">total_vec</span><span class="p">,</span> <span class="n">vector_size</span><span class="p">)))</span>
                    <span class="c1"># store NP vectors in sorted order: most frequent NP&#39;s at the top</span>
                    <span class="k">for</span> <span class="n">word</span><span class="p">,</span> <span class="n">vocab</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span>
                        <span class="n">iteritems</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">wv</span><span class="o">.</span><span class="n">vocab</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">item</span><span class="p">:</span> <span class="o">-</span><span class="n">item</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">count</span>
                    <span class="p">):</span>
                        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_marked</span><span class="p">(</span><span class="n">word</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">word</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>  <span class="c1"># discard empty marked np&#39;s</span>
                            <span class="n">embedding_vec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">wv</span><span class="o">.</span><span class="n">syn0</span><span class="p">[</span><span class="n">vocab</span><span class="o">.</span><span class="n">index</span><span class="p">]</span>
                            <span class="k">if</span> <span class="n">binary</span><span class="p">:</span>
                                <span class="n">fout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">utils</span><span class="o">.</span><span class="n">to_utf8</span><span class="p">(</span><span class="n">word</span><span class="p">)</span> <span class="o">+</span> <span class="sa">b</span><span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="n">embedding_vec</span><span class="o">.</span><span class="n">tostring</span><span class="p">())</span>
                            <span class="k">else</span><span class="p">:</span>
                                <span class="n">fout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
                                    <span class="n">utils</span><span class="o">.</span><span class="n">to_utf8</span><span class="p">(</span>
                                        <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span>
                                        <span class="o">%</span> <span class="p">(</span><span class="n">word</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">val</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">embedding_vec</span><span class="p">))</span>
                                    <span class="p">)</span>
                                <span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">word2vec_format</span><span class="p">:</span>
                    <span class="c1"># pylint: disable=attribute-defined-outside-init</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">KeyedVectors</span><span class="o">.</span><span class="n">load_word2vec_format</span><span class="p">(</span><span class="n">np2vec_model_file</span><span class="p">,</span> <span class="n">binary</span><span class="o">=</span><span class="n">binary</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">word2vec_format</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">np2vec_model_file</span><span class="p">)</span></div>

<div class="viewcode-block" id="NP2vec.load"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.np2vec.NP2vec.load">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">np2vec_model_file</span><span class="p">,</span> <span class="n">binary</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">word_ngrams</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">word2vec_format</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Load the np2vec model.</span>

<span class="sd">        Args:</span>
<span class="sd">            np2vec_model_file (str): the file containing the np2vec model to load</span>
<span class="sd">            binary (bool): boolean indicating whether the np2vec model to load is in binary format</span>
<span class="sd">            word_ngrams (int {1,0}): If 1, np2vec model to load uses word vectors with subword (</span>
<span class="sd">            ngrams) information.</span>
<span class="sd">            word2vec_format(bool): boolean indicating whether the model to load has been stored in</span>
<span class="sd">            original word2vec format.</span>

<span class="sd">        Returns:</span>
<span class="sd">            np2vec model to load</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">word_ngrams</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">word2vec_format</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">KeyedVectors</span><span class="o">.</span><span class="n">load_word2vec_format</span><span class="p">(</span><span class="n">np2vec_model_file</span><span class="p">,</span> <span class="n">binary</span><span class="o">=</span><span class="n">binary</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">KeyedVectors</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">np2vec_model_file</span><span class="p">,</span> <span class="n">mmap</span><span class="o">=</span><span class="s2">&quot;r&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">word_ngrams</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">FastText</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">np2vec_model_file</span><span class="p">)</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;invalid value for &#39;word_ngrams&#39;&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">None</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>